package badugaserver;
import java.io.IOException;
import java.net.ServerSocket;

/**
 * @author Wojtek N., Kamil M.
 * @version 0.7
 * The main class of the server.
 */
public class Server 
{
	protected static Table table;
	protected static ServerSocket server;
	protected static int port = 6789;

	/**
	 * Main testing class of baduga server.
	 * @param args initial parameters taken from terminal (command line).
	 */
	public static void main(String[] args) throws BadugaExceptions
	{
		if (args.length == 4) 
		{
			try 
			{
				int allPlayers = Integer.parseInt(args[0]);
				int startingTokens = Integer.parseInt(args[1]);
				int bigBlind = Integer.parseInt(args[2]);
				int smallBlind = Integer.parseInt(args[3]);
				if(allPlayers<2 || allPlayers>6)
				{
					throw new BadugaExceptions(0);
				}
				if(startingTokens < 100 || startingTokens > 999)
				{
					throw new BadugaExceptions(1);
				}
				if(startingTokens < smallBlind || smallBlind > bigBlind)
				{
					throw new BadugaExceptions(5);
				}
				if(smallBlind < 10 || bigBlind < 11 || smallBlind > 99 || bigBlind > 99)
				{
					throw new BadugaExceptions(6);
				}
				System.out.println("Badugi Server ver. 0.7 \nWelcome! \nServer creates table for " + allPlayers + " players.");
				try 
				{
					server = new ServerSocket(port);
					System.out.println("I am listening on the port " + port+".");
				}
				catch (IOException ioe) 
				{
					System.out.println("I cannot listen on port " + port+".");
				}

				new Listener(allPlayers, startingTokens, smallBlind, bigBlind);
			}
			catch (NumberFormatException nfe) 
			{
				System.err.println("All parameters must be an Integer type!");
			}
		}
		else 
		{
			System.err.println("There are four starting parameters (allPlayers, startingTokens, smallBlind, bigBlind), not less, not more.");
		}

	}
}
